<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/28
 * Time: 20:38
 */

namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;
class Common extends Controller
{
    /**
     * 判断用户是否登录
     */
    public function _initialize()
    {
        //构造方法判断用户是否登录（session是否存在用户ID，登录成功时记录用户ID），没有登录跳转到登录页面
        $userids = session('user_id');
        if (!$userids) {
            $this->redirect('Index/index');
        }
    }

    /**
     * @param $sm
     * 前台用户登录日志
     */
    public function log($sm){
        $filename =Session::get('user').".txt";
        $handle = fopen($filename, "a+");
        //$contents = fread($handle, fielesiz($filename));
        $str="\r\n"."后台用户".Session::get('user')."--".$sm."--".date("Y-m-d H:i:s",time());
        if(fwrite($handle,$str)){
            echo "写入成功";
            $str="\r\n".Session::get('user')."--".$sm."--".date("Y-m-d H:i:s",time());
            if(fwrite($handle,$str)){
                echo '写入成功';
            }else{
                echo "写入失败";
            }
            fclose($handle);

        }
    }
}